package com.learn;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

public class JedisMain {
    public static void main(String[] args) {
        // 连接Redis
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        
        // 测试连接
        System.out.println(jedis.ping());
        
        // 常用API：方法名和命令一样，此处只介绍事务，其他不过多介绍
        // 开启事务
        Transaction multi = jedis.multi();
        try {
            multi.set("key1", "value1");
            // int i = 1/0;
            multi.set("key2", "value2");
            // 执行事务
            multi.exec();
        } catch (Exception e) {
            // 取消事务
            multi.discard();
            throw new RuntimeException(e);
        } finally {
            System.out.println(jedis.get("key1"));
            System.out.println(jedis.get("key2"));
            // 关闭连接
            jedis.close();
        }
    }
}
